package login

import (
	"data/view/mybaidu"
	"data/view/oplog"
	"data/view/user"
	"data/view/verify"
	"data/view/weixin"
	"fmt"
	"public/message"
	"public/tools"
	"time"

	"github.com/ant0ine/go-json-rest/rest"
)

/*
	第三方登录
*/
func ThirdLogin(w rest.ResponseWriter, r *rest.Request) {
	var req weixin.User_wx_info_tbl
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Type, req.Open_id) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	// var db mysqldb.MySqlDB
	// defer db.OnDestoryDB()
	// orm := db.OnGetDBOrm(config.GetDbUrl())

	info := weixin.OnGetUserInfo(req.Open_id, req.Type) //获取基本信息
	var msg LoginToken_s
	msg.MessageBody = message.GetErrorMsg(message.NotBindError, "请先绑定账号")

	if info.Id == 0 || len(info.Username) == 0 { //第一次,保存//已经做过，但是未绑定用户
		weixin.UpdateInfo(req)
	} else { //已经绑定,开始获取accesstoken
		//TODO 学生的话（已删除）不能登录
		username := info.Username
		times := fmt.Sprintf("%d", tools.GetUtcTime(time.Now()))
		msg = GetAccessTokenByUserNpwd(username, times)
		if msg.State {
			//拉取用户角色等信息
			_, b := user.GetUserDetail(username)
			if !b {
				w.WriteJson(message.GetErrorMsg(message.NotFindError))
				return
			}
		}
	}

	w.WriteJson(msg)
}

/*
	绑定手机号
*/
func OnBindPhone(w rest.ResponseWriter, r *rest.Request) {
	var req Req_BindPhone
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Phone, req.Verify) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}

	//手机格式判断
	if !tools.IsPhone(req.Phone) {
		w.WriteJson(message.GetErrorMsg(message.PhoneParameterError))
		return
	}

	//判断手机号是否被绑定
	username := user.OnGetUserNameFromPhone(req.Phone)
	if len(username) > 0 {
		w.WriteJson(message.GetErrorMsg(message.ExistedError, "手机已被绑定"))
		return
	}
	//----------------------end

	//判断用户是否过期
	users, b := user.GetUserDetailFromToken(req.Access_token)
	if b {
		info := verify.OnGet(req.Access_token)
		if len(info.Access_token) == 0 {
			w.WriteJson(message.GetErrorMsg(message.InValidOp, "验证码错误或失效"))
			return
		}

		if info.Phone == req.Phone && info.Verify == req.Verify { //开始绑定
			if user.OnBindPhone(users.User, req.Phone) {
				var logs oplog.Op_log_tbl
				logs.Operator = users.User
				logs.Topic = "帐号系统"
				logs.Bundle = "绑定"
				logs.Pid = "手机"
				logs.Ip_addr = tools.GetClientIp(r.Request)
				logs.Attach0 = info.Phone
				oplog.OpLogBaseAdd(logs) //保存日志
				w.WriteJson(message.GetSuccessMsg())
			} else {
				w.WriteJson(message.GetErrorMsg(message.BindError))
			}
		} else {
			w.WriteJson(message.GetErrorMsg(message.VerifyError))
		}
	} else {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure, "登录已失效,请重新登录"))
	}
}

/*
	通过手机号获取用户信息
*/
func OnGetUserInfoByPhone(w rest.ResponseWriter, r *rest.Request) {
	var req Req_BindPhone
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Phone, req.Verify) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}

	//手机格式判断
	if !tools.IsPhone(req.Phone) {
		w.WriteJson(message.GetErrorMsg(message.PhoneParameterError))
		return
	}

	//判断手机号是否被绑定
	username := user.OnGetUserNameFromPhone(req.Phone)
	if len(username) == 0 {
		w.WriteJson(message.GetErrorMsg(message.NotBindError, "手机未被绑定"))
		return
	}
	//----------------------end

	//获取用户基本信息
	users, b := user.GetUserInfo(username)
	if b {
		info := verify.OnGet(req.Access_token)
		if len(info.Access_token) == 0 {
			w.WriteJson(message.GetErrorMsg(message.InValidOp, "验证码错误或失效"))
			return
		}

		if info.Phone == req.Phone && info.Verify == req.Verify { //开始绑定
			var sex = "男"
			if users.Sex == 2 {
				sex = "女"
			}
			url := mybaidu.GetIdCard(users.Id_card, users.Name, sex,
				users.Nation, users.Addr, users.Birth)
			var res mybaidu.Res_checkOcr
			res.Url = url
			res.Id_card = username
			//开始设置
			var logs oplog.Op_log_tbl
			logs.Operator = users.Id_card
			logs.Topic = "帐号系统"
			logs.Bundle = "修改密码"
			logs.Pid = "手机号获取"
			logs.Ip_addr = tools.GetClientIp(r.Request)
			logs.Attach0 = info.Phone
			msg := message.GetSuccessMsg()
			msg.Data = res
			w.WriteJson(msg)
			oplog.OpLogBaseAdd(logs) //保存日志
		} else {
			w.WriteJson(message.GetErrorMsg(message.VerifyError))
		}
	} else {
		w.WriteJson(message.GetErrorMsg(message.NotFindError, "未找到此用户"))
	}
}

/*
	通过ocr获取用户基本信息
*/
func OnGetUserInfoByPhoto(w rest.ResponseWriter, r *rest.Request) {
	var req Req_CheckUser
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Id_card) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}

	//获取用户信息
	users, b := user.GetUserInfo(req.Id_card)
	if b {
		//开始设置
		var logs oplog.Op_log_tbl
		logs.Operator = users.Id_card
		logs.Topic = "帐号系统"
		logs.Bundle = "修改密码"
		logs.Pid = "ocr获取"
		logs.Ip_addr = tools.GetClientIp(r.Request)
		w.WriteJson(message.GetSuccessMsg())
		oplog.OpLogBaseAdd(logs) //保存日志
	} else {
		w.WriteJson(message.GetErrorMsg(message.NotFindError, "未找到此用户"))
	}
}
